<template>
  <view class="container">
    <!-- 顶部导航栏 -->
    <view class="header">
      <view class="back-btn" @click="backPage()">
        <view class="back-icon"></view>
      </view>
      <text class="title">收支明细</text>
      <view class="options-btn">
        <text class="dot"></text>
        <text class="dot"></text>
        <text class="dot"></text>
      </view>
    </view>
    
    <!-- 余额展示区 -->
    <view class="balance-area">
      <view class="balance-content">
        <!-- 左侧：当前余额 -->
        <view class="current-balance">
          <text class="balance-value">{{wl.current_balance}}</text>
          <text class="balance-label">当前现金余额（元）</text>
        </view>
        
        <!-- 中间：分隔线 -->
        <view class="balance-divider"></view>
        
        <!-- 右侧：累计数据 -->
        <view class="balance-stats">
          <view class="stat-item">
            <text class="stat-label">累计收入（元）：</text>
            <text class="stat-value">{{sumMoney}}</text>
          </view>
          <view class="stat-item">
            <text class="stat-label">累计提现（元）：</text>
            <text class="stat-value">{{wl.cash_withdrawal}}</text>
          </view>
        </view>
      </view>
      
      <!-- 提现按钮 -->
      <view class="withdraw-btn" @click="handleWithdraw()">
        <text>提现</text>
      </view>
    </view>
    
    <!-- 收支明细列表 -->
    <view class="transaction-list">
      <view class="list-title">收支明细</view>
      
      <!-- 交易项目 -->
      <view class="transaction-item" v-for="(item, index) in transactionList" :key="index">
        <view class="transaction-info">
          <text class="transaction-name">{{item.title || '未知交易'}}</text>
          <text class="transaction-time">{{item.time || '未知时间'}}</text>
        </view>
        <text class="transaction-amount" :class="{'income': item.income > 0, 'expense': item.income < 0}">
          {{item.income > 0 ? '+' : '-'}} {{Math.abs(item.income || 0)}}
        </text>
      </view>
    </view>
  </view>
  
  <button @click="getTixian()">hhh</button>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import request from '../../util/request';
import baseUrl from '../../util/urlConfig';

// 交易数据 - 修改为数组类型
const transactionList = ref([]);

// 返回上一页
function backPage(){
  uni.navigateBack()
}

const loginId = uni.getStorageSync("merchant")

const wl = ref({
  id:0,
  merchant_id:0,
  current_balance:0,
  cash_withdrawal:0,
})

const maxMoney = ref(0)

const sumMoney = ref(0)

function getTixian() {
  console.log(11111,loginId.id)
  request({	
    url: baseUrl + "/xyq/withdrawal/"+loginId.id,
    method: "GET",
    success(res) {
      wl.value.id = res.data.data.id
      wl.value.current_balance = res.data.data.currentBalance
      wl.value.merchant_id = res.data.data.merchantId
      wl.value.cash_withdrawal = res.data.data.cashWithdrawal
      sumMoney.value = res.data.data.currentBalance + res.data.data.cashWithdrawal
    }
  })
}

//跳转提现页面
function handleWithdraw(){
  uni.navigateTo({
    url:"/pages/index/shouzhimingxi/tixian/tixian"
  })
}

// 获取收支明细
function getTixianjilu() {
  console.log(11111,loginId.id)
  request({
    url: baseUrl + "/xyq/service-details/"+loginId.id,
    method: "GET",
    success(res) {
      console.log(res)
      // 赋值给transactionList
      if (res.data && res.data.data) {
        // 假设接口返回的数据格式是数组
        if (Array.isArray(res.data.data)) {
          transactionList.value = res.data.data;
        } else {
          // 如果返回的是单个对象，包装成数组
          transactionList.value = [res.data.data];
        }
      }
    }
  })
}

onMounted(()=>{
  getTixian()
  getTixianjilu()
})
</script>

<style>
/* 引入图标库 */
@import "/static/customicons.css";

/* 容器样式 */
.container {
  min-height: 100vh;
  background-color: #f6f6f6;
}

/* 顶部导航栏 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 44px;
  background-color: #2196F3;
  color: #ffffff;
  padding: 0 15px;
  position: relative;
}

.back-btn {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.options-btn {
  display: flex;
  align-items: center;
}

.dot {
  width: 4px;
  height: 4px;
  background-color: #fff;
  border-radius: 50%;
  margin: 0 2px;
}

/* 图标样式 */
.iconfont {
  font-family: "customicons";
  font-size: 24px;
  color: #ffffff;
}

/* 余额展示区 */
.balance-area {
  background: linear-gradient(to bottom, #2196F3, #1E88E5);
  color: #ffffff;
  padding: 20px 15px;
}

.balance-content {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  position: relative;
}

.current-balance {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.balance-value {
  font-size: 36px;
  font-weight: bold;
  margin-bottom: 5px;
}

.balance-label {
  font-size: 14px;
  opacity: 0.9;
}

.balance-divider {
  width: 1px;
  height: 60px;
  background-color: rgba(255, 255, 255, 0.3);
  margin: 0 15px;
}

.balance-stats {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.stat-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.stat-label {
  font-size: 14px;
  opacity: 0.9;
}

.stat-value {
  font-size: 14px;
  font-weight: bold;
}

.withdraw-btn {
  width: 180px;
  height: 40px;
  background-color: #FF5722;
  color: white;
  border-radius: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 16px;
  margin: 20px auto 0;
}

/* 交易列表 */
.transaction-list {
  background-color: #ffffff;
  border-radius: 12px 12px 0 0;
  margin-top: -10px;
  padding: 15px;
  min-height: 300px;
}

.list-title {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin-bottom: 15px;
  padding-left: 5px;
}

.transaction-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 5px;
  border-bottom: 1px solid #f0f0f0;
}

.transaction-info {
  display: flex;
  flex-direction: column;
}

.transaction-name {
  font-size: 14px;
  color: #333;
  margin-bottom: 5px;
}

.transaction-time {
  font-size: 12px;
  color: #999;
}

.transaction-amount {
  font-size: 16px;
  font-weight: bold;
}

.income {
  color: #FF5722;
}

.expense {
  color: #4CAF50;
}

.back-btn{ margin-left: 20rpx; width: 50rpx; height: 50rpx; position: relative;}	
.back-icon { width: 30px; height: 30px;}	
.back-icon::before { content: ""; position: absolute; width: 20px; height: 2px; background-color: #fff; top: 15rpx; left: 47rpx; transform: translate(-50%, -50%); }
.back-icon::after { content: ""; position: absolute; width: 10px; height: 10px; border-left: 2px solid #fff; border-bottom: 2px solid #fff; transform: rotate(45deg); top: 5rpx; left: 30rpx;}	     
</style>


